<?php

/* Portal Pracy - praca magisterska
 * autor: Tomek Ceszke
 * email: tomek.ceszke@gmail.com
 * Politechnika Koszalińska 2007
 */

class Company extends AppModel
{
	var $name = 'Company';
	
	var $belongsTo = array
	(	
		'User' => array
		(
			'className'    => 'User',            
            'dependent'    =>  false,
			'foreignKey'   => 'user_id'
		),
	);
	
	var $hasMany = array
	(
		'Ad' => array
		(
			'className' 	=> 'Ad',
            'foreignKey'    => 'company_id',
            'dependent'     => true,
		)
	);

		
	function loadValidation()
	{		
		$this->validate = array
		(
			'name' => array
      		(
				'required' => array
				(
					'method' => VALID_NOT_EMPTY,
          			'message' => 'Musisz podać nazwę.',
         		),
				'length_within' => array
				(
					'method' => VALID_LENGTH_WITHIN,
          			'message' => 'To pole powinno zawierać od 2 do 50 znaków.',
          			'parameters' => array('min' => 2, 'max' => 50)
				),
				'unique' => array
				(
					'method' => 'uniqueName',
          			'message' => 'Nazwa musi być unikalna.'
				),
			),
           'address' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 200 znaków.',
           			'parameters' => array('max' => 200)
           		)
           ),
           'phones' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 100 znaków.',
           			'parameters' => array('max' => 100)
           		)
           ),
           'fax' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 50 znaków.',
           			'parameters' => array('max' => 50)
           		)
           ),
           'email' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 100 znaków.',
           			'parameters' => array('max' => 100)
           		),
           		'email' => array
           		(
           			'method' => '/\\A(?:^|([a-z0-9][a-z0-9_\\-\\.\\+]*)@([a-z0-9][a-z0-9\\.\\-]{0,63}\\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$)\\z/i',
           			'message' => 'Musisz podać prawidłowy adres email.'
           		)
           ),
           'www' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 100 znaków.',
           			'parameters' => array('max' => 100)
           		),
           		'valid_url' => array
           		(
           			'method' => VALID_URL,
           			'message' => 'Link powinien zaczynać się od: http://.'
	           )           		
           ),
           'career_emails' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 100 znaków.',
           			'parameters' => array('max' => 100)
           		),
           		'email' => array
           		(
           			'method' => '/\\A(?:^|([a-z0-9][a-z0-9_\\-\\.\\+]*)@([a-z0-9][a-z0-9\\.\\-]{0,63}\\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$)\\z/i',
           			'message' => 'Musisz podać prawidłowy adres email.'
           		)
           ),
           'career_phones' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 100 znaków.',
           			'parameters' => array('max' => 100))
           ),
           'career_www' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 100 znaków.',
           			'parameters' => array('max' => 100)
           		),
           		'valid_url' => array
           		(
           			'method' => VALID_URL,
           			'message' => 'Link powinien zaczynać się od: http://.'
	           )  
           ),
           'career_persons' => array
           (           		
           		'length_within' => array
           		(
           			'method' => VALID_LENGTH_WITHIN,
           			'message' => 'To pole powinno zawierać maksymalnie 200 znaków.',
           			'parameters' => array('max' => 200)
           		)
           ),
           'status' => array
           (           		         
				'required' => array 
				(
           			'method' => VALID_NUMBER,
           			'message' => ''
           		),
           	)                           
		);
	}
	
	function uniqueName()
	{
		$name = $this->data['Company']['name'];
		$count = $this->findCount('name=\''.$name.'\'');

		if($count==0)		
		{
			return true;
		}
		else
		{
			return false;
		}
	}

}
?>
